#include <bits/stdc++.h>

using namespace std;

const int MAXN = 1e6 + 10;
int n, k;
int a[MAXN];
struct Tree {
    int m1, m2;
} seg[MAXN * 4];

struct Node {
    int v;
    int id;

    bool operator<(const Node &rhs) const {
        return v > rhs.v;
    }
};

void build(int rt, int l, int r) {
    if (l == r) {
        seg[rt].m1 = a[l];
        seg[rt].m2 = -1;
        return;
    }
    int mid = (l + r) / 2;
    build(rt << 1, l, mid);
    build(rt << 1 | 1, mid + 1, r);
    seg[rt].m1 = max(seg[rt << 1].m1, seg[rt << 1 | 1].m1);
    seg[rt].m2 = max({seg[rt << 1 | 1].m2, seg[rt << 1].m2, min(seg[rt << 1].m1, seg[rt << 1 | 1].m1)});
}

Tree qry(int rt, int l, int r, int ql, int qr) {
    if (l >= ql && r <= qr)
        return seg[rt];
    int mid = (l + r) / 2;
    if (ql <= mid && qr > mid) {
        Tree t1 = qry(rt << 1, l, mid, ql, qr);
        Tree t2 = qry(rt << 1 | 1, mid + 1, r, ql, qr);
        Tree ans;
        ans.m1 = max(t1.m1, t2.m1);
        ans.m2 = max({t1.m2, t2.m2, min(t1.m1, t2.m1)});
        return ans;
    } else if (ql <= mid) return qry(rt << 1, l, mid, ql, qr);
    else return qry(rt << 1 | 1, mid + 1, r, ql, qr);
}

int main() {
    //freopen("in.txt", "r", stdin);
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
    if (k == 1) {
        puts("0");
        return 0;
    }
    build(1, 1, n);
    int ans = 100000000;
    for (int i = 1; i + k - 1 <= n; ++i) {
        int j = i + k - 1;
        Tree tmp = qry(1, 1, n, i, j);
        ans = min(ans, tmp.m1 - tmp.m2);
    }

    printf("%d\n", ans);

    return 0;
}